package com.yuexiao.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yuexiao.entity.PageQueryInfo;
import com.yuexiao.entity.ResultInfo;
import com.yuexiao.pojo.Employee;
import com.yuexiao.service.EmployeeService;
import com.yuexiao.util.Tool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName:EmpolyeeController
 * @Auther: YUEXIAO
 * @Description: 员工操作实体类
 * @Date: 2022/8/10 22:29
 * @Version: v1.0
 */
@RestController
@RequestMapping("/employee")
@Slf4j
public class EmpolyeeController {

    @Reference
    private EmployeeService employeeService;

    /**
     * 根据条件分页查询员工列表信息
     * @param page int 第几页
     * @param pageSize int 每页显示条数
     * @param name String 查询条件:员工姓名
     * @return ResultInfo 结果对象
     */
    @GetMapping("/page")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_USER')")
    public ResultInfo page(int page,int pageSize,String name){
        log.info("page = {},pageSize = {},name = {}" ,page,pageSize,name);
        return ResultInfo.success(employeeService.getEmployeeByPages(page,pageSize,name));
    }


    /**
     * 根据id查询员工信息
     * @param id Long 员工主键id
     * @return ResultInfo 返回结果对象
     */
    @GetMapping("/queryEmployeeById/{id}")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_USER')")
    public ResultInfo queryEmployeeById(@PathVariable Long id){
        Employee employeeById = employeeService.getEmployeeById(id);
        return ResultInfo.success(employeeById);
    }


    /**
     * 添加员工数据
     * @param request 无需手动传参，自动传入
     * @param employee Employee 员工实体类
     * @return ResultInfo 返回结果对象
     */
    @PostMapping("/save")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_USER')")
    public ResultInfo save(HttpServletRequest request, @RequestBody Employee employee){
        log.info("新增员工，员工信息：{}",employee.toString());

        //设置初始密码(123456)
        employee.setPassword(Tool.getEncoder("123456"));

        //设置默认权限
        employee.setRole("ROLE_USER");

        boolean save = employeeService.save(employee);
        if(save){
            return ResultInfo.success(null);
        }
        return ResultInfo.error("新增失败");
    }


    /**
     * 编辑用户信息
     * @param request 无需手动传入
     * @param employee Employee 员工对象
     * @return ResultInfo 返回结果对象
     */
    @PutMapping("/edit")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_USER')")
    public ResultInfo edit(HttpServletRequest request,@RequestBody Employee employee){
        log.info(employee.toString());
        //执行更新
        boolean update = employeeService.update(employee);

        if(update){
            return ResultInfo.success(null);
        }

        return ResultInfo.error("更新失败");
    }

    /**
     * 更新账户状态
     * @param request 无需手动传入
     * @param employee Employee 员工对象
     * @return ResultInfo 返回结果对象
     */
    @PutMapping("/enableOrDisableEmployee")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN')")
    public ResultInfo enableOrDisableEmployee(HttpServletRequest request,@RequestBody Employee employee){
        log.info(employee.toString());
        //更新数据
        boolean update = employeeService.update(employee);
        if(update){
            return ResultInfo.success(null);
        }
        return ResultInfo.error("状态更新失败");
    }
}
